package com.yami.shop.api.controller;

import com.alibaba.fastjson.JSON;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.bean.model.TransferRecord;
import com.yami.shop.bean.model.User;
import com.yami.shop.bean.param.SendUserDropsParam;
import com.yami.shop.bean.param.UserUpdateParam;
import com.yami.shop.common.annotation.RedisLock;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.StringUtils;
import com.yami.shop.service.BusinessNftService;
import com.yami.shop.service.SysConfigService;
import com.yami.shop.service.TransferRecordService;
import com.yami.shop.service.UserService;
import com.yami.shop.webservice.common.util.IpUtil;
import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/prod")
public class TransferRecordController {


    @Autowired
    private TransferRecordService transferRecordService;
    @Autowired
    private UserService userService;
    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private BusinessNftService businessNftService;

    /**
     * 划转接口
     */
    @RedisLock(lockName = "prodTransferInsert",key ="#TransferRecord")
    @PostMapping("/transfer")
    public ServerResponseEntity<Boolean> insert(@RequestBody TransferRecord airdropTaskConditions, HttpServletRequest request) {
        // 获取请求IP校验
        String ipAddr = IpUtil.getIpAddr(request);
        String value = sysConfigService.getValue("REQUEST_IP");
        if(StringUtils.isEmpty(value)){
            return ServerResponseEntity.showFailMsg("暂无IP配置");
        }
        List<String> ips = JSON.parseArray(value, String.class);
        if(!ips.contains(ipAddr)){
            return ServerResponseEntity.showFailMsg("非法请求");
        }
        airdropTaskConditions.setStatus("0");
        // 判断用户是否存在
        User userDetailByCallPhone = userService.getUserDetailByCallPhone(airdropTaskConditions.getPhone());
        if(Objects.isNull(userDetailByCallPhone)){
            return ServerResponseEntity.showFailMsg("该用户不存在");
        }
        if(StringUtils.isEmpty(userDetailByCallPhone.getIdNumber())){
            return ServerResponseEntity.showFailMsg("该账号未实名");
        }
        if(!userDetailByCallPhone.getIdNumber().equals(airdropTaskConditions.getIdNum())){
            return ServerResponseEntity.showFailMsg("该账号实名信息不符合");
        }
        // 是否系统自动审核
        String transfer = sysConfigService.getValue("PROD_TRANSFER");
        // 空投
        if("1".equals(transfer)){
            airdropTaskConditions.setStatus("1");
            String prodId = sysConfigService.getValue("KT_PROD_ID");
            if(StringUtils.isEmpty(prodId)){
                return ServerResponseEntity.showFailMsg("系统配置异常");
            }
            this.transferRecordService.save(airdropTaskConditions);
            UserUpdateParam param = new UserUpdateParam();
            List<SendUserDropsParam> userDropsParams = new ArrayList<>();
            SendUserDropsParam sendUserDropsParam = new SendUserDropsParam();
            sendUserDropsParam.setTargetId(Long.parseLong(prodId));
            sendUserDropsParam.setType(0);
            sendUserDropsParam.setNums(airdropTaskConditions.getNum().intValue());
            userDropsParams.add(sendUserDropsParam);
            param.setDrops(userDropsParams);
            param.setUserIds(Collections.singletonList(userDetailByCallPhone.getUserId()));
            param.setBusinessType(99);
            param.setMainProdId(Long.parseLong(prodId));
            param.setAirdropBusinessId(airdropTaskConditions.getId());
            ShopDetail shopDetail = new ShopDetail();
            shopDetail.setShopId(999L);
            businessNftService.bindDropById(param, shopDetail);
        }
        return ServerResponseEntity.success();
    }


}
